package com.example.gokchinesefoodmapmcdev.controller;

import com.example.gokchinesefoodmapmcdev.entity.goods.GoodsInfo;
import com.example.gokchinesefoodmapmcdev.entity.membercart.MemberCartItem;
import com.example.gokchinesefoodmapmcdev.service.MemberCartService;
import com.example.gokchinesefoodmapmcdev.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.parameters.P;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/rest/memberCart")
public class MemberCartController {

    @Autowired
    private MemberCartService memberCartService;

    /**
     * 将商品添加进购物车
     * @return
     */
    @PostMapping("/save")
    public ResultData addMemberCart(@RequestBody Map<String, Object> map){
        String productDesc =(String) map.get("productDesc");
        String productName = (String) map.get("productName");
        Integer goodId = (Integer) map.get("goodId");
        String attrSymbolPath =(String) map.get("attrSymbolPath");
        Integer merchantId = (Integer)map.get("merchantId");
        Integer number = (Integer)map.get("number");
        // 将传入的数据（字段不匹配），设置到实体类中
        GoodsInfo goodsInfo = new GoodsInfo();
        goodsInfo.setId(goodId);
        goodsInfo.setGoodDesc(productDesc);
        goodsInfo.setGoodName(productName);
        goodsInfo.setMerchantId(merchantId.longValue());

        Integer result = memberCartService.addMemberCart(goodsInfo, number, attrSymbolPath);
        if(result > 0){
            return ResultData.fail(ResultData.OPERATION_SUCCESS, "操作成功");
        }
        return ResultData.fail(ResultData.OPERATION_FAIL, "操作失败");
    }

    /**
     * 购物车物品的删除
     * @param ids
     * @return
     */
    @PostMapping("/remove")
    public ResultData removeMemberCartItem(Integer[] ids){
        Integer result = memberCartService.removeMemberCartItem(ids);
        if(result > 0){
            return ResultData.ok(ResultData.OPERATION_SUCCESS, "操作成功!");
        }
        return ResultData.fail(ResultData.OPERATION_FAIL, "操作失败，请稍后再试！");
    }


    /**
     * 修改购物车项的规格和数量
     *      前提：用户选择的购物车信息在数据库中要存在数据
     *  memberCartItem    购物车部分信息（购物车项id，商品Id，商品规格文字）
     *  attrSymbolPath    商品规格码组合，字符串，按规格排序用逗号隔开
     *  number            商品数量
     * @return
     */
    @PostMapping("/update")
    public ResultData updateMemberCart(@RequestBody Map<String, Object> map){
        MemberCartItem memberCartItem = new MemberCartItem();
        memberCartItem.setId((Integer)map.get("id"));
        memberCartItem.setGoodId((Integer)map.get("goodId"));
        memberCartItem.setProductDesc((String)map.get("productDesc"));

        String attrSymbolPath = (String) map.get("attrSymbolPath");
        Integer number = (Integer) map.get("number");

        Date gmtUpdated = new Date();
        memberCartItem.setGmtUpdated(gmtUpdated);

        Integer result = memberCartService.updateMemberCartItem(memberCartItem, attrSymbolPath, number);
        if(result > 0){
            return ResultData.ok(ResultData.OPERATION_SUCCESS, "操作成功");
        }else if(result == -1){
            return ResultData.fail(ResultData.OPERATION_FAIL, "库存不足，或当前库存中无此商品规格信息");
        }
        return ResultData.fail(ResultData.OPERATION_FAIL, "操作失败");
    }

    /**
     * 查询当前用户的购物车项
     */
    @GetMapping("/list")
    public ResultData getMemberCartList(){
        // 取出当前用户简单信息
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        List<Map<String, Object>> result = memberCartService.findMemberCartItemByUser(userDetails.getUsername());
        if(result.isEmpty()){
            return ResultData.fail(ResultData.OPERATION_FAIL, "您还没有添加任何商品到购物车中");
        }

        ResultData resultData = new ResultData();
        resultData.put("code", ResultData.OPERATION_SUCCESS);
        resultData.put("rows", result);
        return resultData;
    }


}
